package com.itheima.service;

import com.alibaba.dubbo.config.annotation.Service;
import com.itheima.dao.OrderSettingDao;
import com.itheima.pojo.OrderSetting;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;


import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service(interfaceClass = OrderSetttingService.class)
@Transactional
public class OrderSettingServiceImpl implements OrderSetttingService {
    @Autowired
    private OrderSettingDao orderSettingDao;
    @Override
/**
 * 添加预约服务
 * 添加之前要先判断数据库是否有数据
 * 有就不添加 执行更新操作  没有就新添加
 */
    public void add(List<OrderSetting> data) {
        if (data!=null&&data.size()>0){
            for (OrderSetting odersetting : data) {
                long count=orderSettingDao.findCountOrderDate(odersetting.getOrderDate());
                if (count>0){
                    orderSettingDao.editByOrderDate(odersetting);
                }else {
                    orderSettingDao.add(odersetting);
                }
            }
        }
    }
    /**
     * 根据指定日期来获取所设置的数据
     *
     * @return
     */
    @Override
    public List<Map> getOrderSettingByMonth(String date) {
        List<Map> orderSettings=orderSettingDao.getOrderSettingByMonth(date);
        return orderSettings;
    }


    /**
     * 根据日期来修改预约人数
     *
     * @return
     */
    @Override
    public void edit(OrderSetting orderSetting) {
        long countOrderDate = orderSettingDao.findCountOrderDate(orderSetting.getOrderDate());
        if (countOrderDate>0)
        {
            orderSettingDao.editByOrderDate(orderSetting);
        }else{
            orderSettingDao.add(orderSetting);
        }
    }
}
